package com.jt61016.ch04.deepclone2;

import java.io.*;

public class ResumeDeepClone implements Serializable {
    private static final long serialVersionUID = -6045269019958636531L;

    /**
     * 姓名
     */
    private String name;
    /**
     * 性别
     */
    private String sex;
    /**
     * 年龄
     */
    private Integer age;

    /**
     * 工作经历
     */
    private WorkExperience workExperience;

    public void setWorkExperience(WorkExperience workExperience) {
        this.workExperience = workExperience;
    }

    public WorkExperience getWorkExperience() {
        return workExperience;
    }

    public ResumeDeepClone(String name) {
        this.name = name;
        this.workExperience = new WorkExperience();
    }

    public void setPersonalInfo(String sex, Integer age) {
        this.sex = sex;
        this.age = age;
    }

    public void setWorkExperience(String timeArea, String company) {
        workExperience.setTimeArea(timeArea);
        workExperience.setCompany(company);
    }

    @Override
    public ResumeDeepClone clone() {
        // 序列化
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        // 对象输入流
        ObjectOutputStream objectOutputStream = null;
        try {
            objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
            objectOutputStream.writeObject(this);
        } catch (IOException e) {
            System.out.println(e.getStackTrace());
        }

        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
        // 对象输出流
        ObjectInputStream objectInputStream = null;
        try {
            objectInputStream = new ObjectInputStream(byteArrayInputStream);
            return (ResumeDeepClone) objectInputStream.readObject();
        } catch (IOException e) {
            System.out.println(e.getStackTrace());
        } catch (ClassNotFoundException e) {
            System.out.println(e.getStackTrace());
        }

        return null;
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append(name)
                .append(" ")
                .append(sex)
                .append(" ")
                .append(age)
                .append(" 工作经历：")
                .append(workExperience.getTimeArea())
                .append(" ")
                .append(workExperience.getCompany());
        return sb.toString();
    }
}
